package model

import (
	"stock/global"
)

type UserModel struct {
	Base
}

type MallUser struct {
	Id            uint   `json:"id" gorm:"<-:false;primaryKey;column:id;"`
	Account       string `json:"account" gorm:"<-:false; column:account;type:"`
	Password      string `json:"-" gorm:"column:password;"`
	Salt          string `json:"salt" gorm:"column:salt;"`
	IsDelete      uint8  `json:"is_delete" gorm:"column:is_delete;"`
	UpdateTime    uint64 `json:"update_time" gorm:"column:update_time;"`
	CreateTime    uint64 `json:"create_time" gorm:"column:create_time;"`
	Status        uint8  `json:"status" gorm:"column:status;"`
	Role          uint8  `json:"role" gorm:"column:role;"`
	LastLoginTime uint64 `json:"last_login_time" gorm:"column:last_login_time;"`
}

func (m *UserModel) TabName() string {
	return global.CONF.Mysql.Prefix + "mall_user"
}

type RequestUser struct {
	Account    string
	IsDelete   uint8
	CreateTime uint64
	Status     uint8
	Role       uint8
}

type ResponseUser struct {
	MallUser
	UserId uint `json:"user_id"`
}

func (m *UserModel) Detail(param RequestUser) (MallUser, error) {

	where := &MallUser{}
	if param.Account != "" {
		where.Account = param.Account
	}
	if param.IsDelete != 0 {
		where.IsDelete = param.IsDelete
	}
	if param.CreateTime != 0 {
		where.CreateTime = param.CreateTime
	}
	if param.Status != 0 {
		where.Status = param.Status
	}
	if param.Role != 0 {
		where.Role = param.Role
	}
	var user MallUser
	err := global.DB.Debug().Where(where).Order("id DESC").Find(&user)
	if err.Error != nil {
		return user, err.Error
	}
	return user, nil
}
